<?php
function menu_model_get_child_ids($menu_id = 0) {
	$menu_id = intval($menu_id);
	$menu_ids_array = menu_model_get_child_ids_array($menu_id);
	$menu_ids = implode(',', $menu_ids_array);
	return $menu_ids;

}

function menu_model_get_child_ids_array($menu_id = 0) {
	$menu_id = intval($menu_id);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select id from menus where parent_id=" . $menu_id;
	$menus_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			while ($row = mysqli_fetch_assoc($result)) {
				$menus_array[] = $row['id'];
				$menu_child_ids = menu_model_get_child_ids_array($row['id']);
				$menus_array = array_merge($menus_array, $menu_child_ids);
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $menus_array;
}

function menu_model_insert($menu_name = '', $menu_url = '', $parent_id = 0, $status = 1, $keywords = '', $description = '') {

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$menu_name = trim($menu_name);
	$menu_url = trim($menu_url);
	$create_time = date('Y-m-d H:i:s', time());
	$hash = md5($create_time);
	$status = intval($status);
	$parent_id = intval($parent_id);
	$sql = "insert into menus(hash,parent_id,menu_url,menu_name,status)values(?,?,?,?,?)";

	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {
			printf("MYSQL ERROR: %s\n", mysqli_error($db));
			return false;
		}

		if (!$stmt->bind_param('sissi', $hash, $parent_id, $menu_url, $menu_name, $status)) {
			$stmt->close();

			return false;
		}

		if (!$stmt->execute()) {
			$stmt->close();

			return false;
		} else {
			$id = $db->insert_id;
			$stmt->close();

			return $id;
		}

	} catch (Exception $e) {

		// TODO
		echo $e;
		return false;
	}
}

function menu_model_get_by_id($menu_id = 0) {
	if ($menu_id == 0) {
		return false;
	}

	$menu_id = intval($menu_id);

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from menus where `id`='" . $menu_id . "' limit 1";
	$menu_data = false;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			$menu_data = mysqli_fetch_assoc($result);
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $menu_data;
}

/**
 * 根据hash获取数据
 *
 * @param  string
 * @return [type]
 */
function menu_model_get_by_hash($hash = '') {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from menus where `hash`='" . $hash . "' limit 1";
	$menu_data = false;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			require_once APP_PATH . '/menu/helper.php';
			require_once APP_PATH . '/post/helper.php';
			$menu_data = mysqli_fetch_assoc($result);
			if (!empty($menu_data)) {
				//获取分类信息
				$menu = menu_helper_get_menu($menu_data['parent_id']);
				$menu_data['parent_menu_name'] = $menu['menu_name'];
			} else {
				$menu_data = false;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $menu_data;
}

/**
 * 触发系统生成纯静态化页面业务逻辑
 * 有3种方案：
 * 第一：定时扫描程序(利用crontab来处理)
 * 第二：手动触发方式，人为触发
 * 第三：页面添加缓存时间，在页面中控制时间来操作
 */
function menu_model_get_list($page = 1, $page_size = 8) {
	$page = intval($page);
	$limit = ($page - 1) * $page_size;

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from menus where `status` = 1 limit " . $limit . "," . $page_size;
	$menus_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			while ($row = mysqli_fetch_assoc($result)) {
				$row['parent_menu_name'] = menu_helper_get_menu($row['parent_id']);
				$menus_array[] = $row;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $menus_array;
}

function menu_model_get_all_list() {
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	require_once APP_PATH . 'menu/helper.php';
	require_once APP_PATH . 'post/helper.php';
	$sql = "select * from menus";
	$menus_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			while ($row = mysqli_fetch_assoc($result)) {
				$parent_menu_name = menu_helper_get_menu($row['parent_id']);
				$row['parent_menu_name'] = $parent_menu_name['menu_name'];
				$row['status'] = ($row['status'] == 1) ? '已发布' : '隐藏';

				$menus_array[] = $row;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $menus_array;
}

function menu_model_get_all_list_count($menu_id = 0, $keyword = '') {
	$where = " 1=1 ";

	//数据安全过滤
	$keyword = trim($keyword);
	//分类
	$menu_id = intval($menu_id);
	require_once APP_PATH . DIRECTORY_SEPARATOR . 'menu/model.php';
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	if (!empty($menu_id)) {
		//获取下级所有分类ID
		$menu_child_ids = menu_model_get_child_ids($menu_id);
		$menu_ids = "";
		//分类ID通过,链接
		if (empty($menu_child_ids)) {
			$menu_ids = $menu_id;
		} else {
			$menu_ids = $menu_child_ids . ',' . $menu_id;
		}

		$where .= "and menu_id in (" . $menu_ids . ") ";
	}

	if (!empty($keyword)) {
		$where .= "and (menu_name like '%" . $keyword . "%' or menu_url like '%" . $keyword . "%')";
	}

	//sql查询
	$sql = "select count(id) as count from menus where " . $where;
	$result_count = 0;
	try
	{
		//创建实力
		$db = Db::getInstance()->connect();
		//查询
		$result = mysqli_query($db, $sql);
		//结果处理
		if ($result) {
			$row = mysqli_fetch_assoc($result);
			if ($row) {
				$result_count = $row['count'];
			}

		} else {
			//SQL报错停止
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// 其他异常显示
	}
	//返回
	return $result_count;
}

function menu_model_delete_by_hash($hash = '') {
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$hash = filter_data($hash);
	if (empty($hash)) {
		return false;
	}
	$menu_data = menu_model_get_by_hash($hash);

	//删除数据库记录
	$sql = "delete from menus where hash=?";
	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {

			return false;
		}

		if (!$stmt->bind_param('s', $hash)) {

			$stmt->close();

			return false;
		}

		if (!$stmt->execute()) {

			$stmt->close();

			return false;
		} else {
			$stmt->close();
			return true;
		}
	} catch (Exception $e) {
		// TODO
		echo $e;
		return false;
	}
}

function menu_model_update_by_hash($hash = '', $menu_name, $parent_id = 0, $menu_url = '', $status = 0) {

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$hash = filter_data($hash);
	if (empty($hash)) {
		return false;
	}

	$menu_name = trim($menu_name);
	$modify_time = date('Y-m-d H:i:s', time());
	$status = intval($status);
	$sql = "update menus set parent_id=?,menu_name=?,menu_url=?,modify_time=?,status=? where hash=?";
	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {
			echo $db->error;
			return false;
		}

		if (!$stmt->bind_param('isssis', $parent_id, $menu_name, $menu_url, $modify_time, $status, $hash)) {

			$stmt->close();

			return false;
		}

		$menu_path = strtolower('/menu/' . $menu_url);

		if (!$stmt->execute()) {

			$stmt->close();

			return false;
		} else {
			$stmt->close();

			require_once APP_PATH . '/menu/build.php';
			//更新HTML
			menu_build_page_by_hash($hash);
			return true;
		}
	} catch (Exception $e) {
		// TODO
		echo $e;
	}
}

/**
 * 根据hash更新统计信息
 *
 * @param  string
 * @return [type]
 */
function menu_model_update_is_build_by_hash($hash = '', $is_build = 1) {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	$is_build = intval($is_build);
	$build_time = date('Y-m-d H:i:s', time());
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "update menus set is_build='" . $is_build . "' ,build_time='" . $build_time . "'  where `hash`='" . $hash . "'";
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);
		if ($result) {
			return true;
		} else {
			return false;
		}
	} catch (Exception $e) {
		// TODO
		return false;
	}
	return false;
}
